/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.uri;

import me.panpf.sketch.datasource.ContentDataSource;
import me.panpf.sketch.datasource.DataSource;
import me.panpf.sketch.request.DownloadResult;
import ohos.agp.utils.TextTool;
import ohos.app.Context;
import ohos.utils.net.Uri;

public class OhosResUriModel extends UriModel {

    public static final String SCHEME = "resources/";

    /**
     * 根据资源名称和类型创建 uri
     *
     * @param packageName     包名
     * @param resType         资源类型，例如 "drawable" 或 "mipmap"
     * @param drawableResName 图片资源名称
     * @return 例如：resource/base/graphic/ic_play
     */
    public static String makeUriByName(String packageName, String resType, String drawableResName) {
        return SCHEME + packageName + "/" + resType + "/" + drawableResName;
    }

    /**
     * 根据资源 ID 创建 uri
     *
     * @param packageName   包名
     * @param drawableResId 图片资源ID
     * @return 例如：resource://me.panpf.sketch.sample/1031232
     */
    public static String makeUriById(String packageName, int drawableResId) {
        return SCHEME + packageName + "/" + drawableResId;
    }

    @Override
    protected boolean match(String uri) {
        return !TextTool.isNullOrEmpty(uri) && uri.startsWith(SCHEME);
    }

    @Override
    public DataSource getDataSource(Context context, String uri, DownloadResult downloadResult) {
        return new ContentDataSource(context, Uri.parse(uri));
    }
}
